package com.lenovo.smarttraffic.ui.activity.stage4;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.MyVpAdapter;
import com.lenovo.smarttraffic.ui.fragment.stage4.UserCenterFragment;

import java.util.ArrayList;
import java.util.List;

public class UserCenterActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    private List<Fragment> fragments = new ArrayList<>();

    private Toolbar mToolbar;
    private DrawerLayout mDrawer;
    private NavigationView navigationView;

    private TextView user_info, charge_hist, threshold_set;
    private ViewPager user_vp;

    private Handler handler = new Handler();

    @Override
    protected int getLayout() {
        return R.layout.activity_user_center;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }


    public void initView() {
        mToolbar = findViewById(R.id.toolbar_four);

        mDrawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        user_vp = findViewById(R.id.user_vp);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();
        mDrawer.addDrawerListener(toggle);

        user_info = findViewById(R.id.user_info);
        user_info.setOnClickListener(v -> {
            user_info.setBackgroundResource(R.drawable.item_indicate);
            charge_hist.setBackground(null);
            threshold_set.setBackground(null);
            user_vp.setCurrentItem(0, false);
        });
        charge_hist = findViewById(R.id.charge_hist);
        charge_hist.setOnClickListener(v -> {
            user_info.setBackground(null);
            charge_hist.setBackgroundResource(R.drawable.item_indicate);
            threshold_set.setBackground(null);
            user_vp.setCurrentItem(1, false);
        });
        threshold_set = findViewById(R.id.threshold_set);
        threshold_set.setOnClickListener(v -> {
            user_info.setBackground(null);
            charge_hist.setBackground(null);
            threshold_set.setBackgroundResource(R.drawable.item_indicate);
            user_vp.setCurrentItem(2, false);
        });

    }

    public void initData() {
        fragments.add(new UserCenterFragment().setType(0));
        fragments.add(new UserCenterFragment().setType(1));
        fragments.add(new UserCenterFragment().setType(2));

        user_vp.setOffscreenPageLimit(fragments.size());
        user_vp.setAdapter(new MyVpAdapter(getSupportFragmentManager(), fragments));
        user_vp.setAnimation(null);
        user_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        user_info.setBackgroundResource(R.drawable.item_indicate);
                        charge_hist.setBackground(null);
                        threshold_set.setBackground(null);
                        break;
                    case 1:
                        user_info.setBackground(null);
                        charge_hist.setBackgroundResource(R.drawable.item_indicate);
                        threshold_set.setBackground(null);
                        break;
                    case 2:
                        user_info.setBackground(null);
                        charge_hist.setBackground(null);
                        threshold_set.setBackgroundResource(R.drawable.item_indicate);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    @Override
    public void onBackPressedSupport() {
        if (mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START);
        } else finish();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        mDrawer.closeDrawer(GravityCompat.START);

        handler.postDelayed(() -> {
            int id = item.getItemId();
            switch (id) {
                case R.id.account_manage:
                    startActivity(new Intent(this, AccountManageActivity.class));
                    break;
                case R.id.data_analyze:
                    startActivity(new Intent(this, DataAnalyzeActivity.class));
                    break;
                case R.id.traffic_manage:
                    startActivity(new Intent(this, TrafficManageActivity.class));
                    break;
                case R.id.qrcode_pay:
                    startActivity(new Intent(this, QRCodePayActivity.class));
                    break;
                case R.id.custom_bus:
                    startActivity(new Intent(this, CustomBusActivity.class));
                    break;
                case R.id.subway_find:
                    startActivity(new Intent(this, SubwayFindActivity.class));
                    break;
                case R.id.realtime_traffic:
                    startActivity(new Intent(this, RealTrafficActivity.class));
                    break;
            }
        }, 300);
        return true;
    }
}
